package com.kifas.service.dataaccess;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.AkunMasterEntity;

@Repository
public class AkunMasterDao {

	private static final String ALL_AKUN_MASTER = "from AkunMasterEntity am ";
	private static final String AKUN_MASTER_BY_KODE = ALL_AKUN_MASTER + "where am.kodeAkun = :kodeAkun";
	private static final String AKUN_MASTER_WHERE_IN = ALL_AKUN_MASTER + "where am.kodeAkun in (:list) order by am.kodeAkun asc";
	private static final String GET_AKUN_LEAF = "SELECT t1.kd_akun FROM tb_master_akun AS t1 " +
			"LEFT JOIN tb_master_akun as t2 ON t1.kd_akun = t2.id_parent_akun " +
			"WHERE t2.kd_akun IS NULL and t1.is_active = 1";
	private static final String GET_AKUN_LEAF_DEBET = "SELECT t1.kd_akun FROM tb_master_akun AS t1 " +
			"LEFT JOIN tb_master_akun as t2 ON t1.kd_akun = t2.id_parent_akun " +
			"WHERE t2.kd_akun IS NULL and t1.dk = '1' and t1.is_active = 1";
	private static final String GET_AKUN_LEAF_KREDIT = "SELECT t1.kd_akun FROM tb_master_akun AS t1 " +
			"LEFT JOIN tb_master_akun as t2 ON t1.kd_akun = t2.id_parent_akun " +
			"WHERE t2.kd_akun IS NULL and t1.dk = '0' and t1.is_active = 1";

	@Autowired
	private SessionFactory sessionFactory;

	public void insert(AkunMasterEntity akunMaster) throws Exception {
		sessionFactory.getCurrentSession().save(akunMaster);
	}

	public void update(AkunMasterEntity akunMaster) throws Exception {
		sessionFactory.getCurrentSession().update(akunMaster);
	}

	@SuppressWarnings("unchecked")
	public List<AkunMasterEntity> getAkunMasters() {
		return sessionFactory.getCurrentSession().createQuery(ALL_AKUN_MASTER + " where am.active = true order by am.kodeAkun asc")
				.list();
	}

	public AkunMasterEntity getAkunMaster(String kodeAkun) {
		return (AkunMasterEntity) sessionFactory.getCurrentSession()
				.createQuery(AKUN_MASTER_BY_KODE + " and am.active = true")
				.setString("kodeAkun", kodeAkun).uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<AkunMasterEntity> getAkunLeaf() {
		List<Object> list = sessionFactory.getCurrentSession().createSQLQuery(GET_AKUN_LEAF).list();
		List<String> criteria = new ArrayList<String>();
		for(Object o : list) {
			criteria.add((String)o);
		}
		return sessionFactory.getCurrentSession().createQuery(AKUN_MASTER_WHERE_IN).setParameterList("list", criteria).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<AkunMasterEntity> getAkunLeafDebet() {
		List<Object> list = sessionFactory.getCurrentSession().createSQLQuery(GET_AKUN_LEAF_DEBET).list();
		List<String> criteria = new ArrayList<String>();
		for(Object o : list) {
			criteria.add((String)o);
		}
		return sessionFactory.getCurrentSession().createQuery(AKUN_MASTER_WHERE_IN).setParameterList("list", criteria).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<AkunMasterEntity> getAkunLeafKredit() {
		List<Object> list = sessionFactory.getCurrentSession().createSQLQuery(GET_AKUN_LEAF_KREDIT).list();
		List<String> criteria = new ArrayList<String>();
		for(Object o : list) {
			criteria.add((String)o);
		}
		return sessionFactory.getCurrentSession().createQuery(AKUN_MASTER_WHERE_IN).setParameterList("list", criteria).list();
	}

}
